package com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.DO;

import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.SentinelDegradeRuleDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.io.Serializable;

/**
 * xx
 *
 * @author BugFree Jin
 * @version 1.0
 * @since 2022/3/2 15:45
 */
@Data
@Table(value = "sentinel_degrade_rule")
public class SentinelDegradeRuleDO extends BaseDO {
    private static final long serialVersionUID = -2514008591675348263L;
    @Id
    private long id;

    @Column("degrade_rule")
    private String degradeRule;

    public SentinelDegradeRuleDTO getSentinelDegradeRuleDTO() {
        SentinelDegradeRuleDTO sentinelDegradeRuleDTO = new SentinelDegradeRuleDTO();
        try {
            DegradeRule dr = new ObjectMapper().readValue(this.degradeRule, DegradeRule.class);
            BeanUtils.copyProperties(dr, sentinelDegradeRuleDTO);
            BeanUtils.copyProperties(this, sentinelDegradeRuleDTO, "degradeRule");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return sentinelDegradeRuleDTO;
    }

    @Data
    public static class DegradeRule implements Serializable {

        private Double count;
        private Integer grade;
        private Integer minRequestAmount;
        private Integer rtSlowRequestAmount;
        private Integer timeWindow;
        private String limitApp;
        private String resource;

        private static final long serialVersionUID = -6354266610977165782L;
    }

}
